// Wave.cpp : implementation file   
//   
   
#include "stdafx.h"   
#include "QMWave.h"   
   
#ifdef _DEBUG   
#define new DEBUG_NEW   
#undef THIS_FILE   
static char THIS_FILE[] = __FILE__;   
#endif   
   
/////////////////////////////////////////////////////////////////////////////   
// CQMWave   
   
CQMWave::CQMWave()    
{    
    m_nDeviceID=0;    
    m_nElementID=0;   
}    
   
CQMWave::~CQMWave()    
{    
    if(m_nElementID)   
        Stop();    
    if(m_nDeviceID)    
        CloseDevice();    
}    
   
DWORD CQMWave::OpenDevice()    
{    
    DWORD dwResult=0;    
    if ( !m_nDeviceID )   
    {   
        MCI_OPEN_PARMS mciOpenParms;    
        mciOpenParms.lpstrDeviceType=(LPTSTR)MCI_DEVTYPE_WAVEFORM_AUDIO;    
   
        dwResult=mciSendCommand(NULL,MCI_OPEN,    
            MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID|MCI_WAIT,    
            (DWORD)(LPVOID)&mciOpenParms);    
   
        m_nDeviceID=mciOpenParms.wDeviceID;    
   
        if(dwResult)    
            DisplayErrorMsg(dwResult);    
    }    
    return dwResult;    
}    
   
DWORD CQMWave::CloseDevice()    
{    
    DWORD dwResult=0;    
   
    if(m_nDeviceID)    
    {   
        dwResult=mciSendCommand(m_nDeviceID,MCI_CLOSE,NULL,NULL);    
   
        if(dwResult)    
            DisplayErrorMsg(dwResult);   
        else   
            m_nDeviceID=0;    
    }    
    return dwResult;    
}    
   
DWORD CQMWave::Play(HWND hWnd,LPCTSTR pFileName)    
{    
    MCI_OPEN_PARMS mciOpenParms;    
    memset(&mciOpenParms,0,sizeof(MCI_OPEN_PARMS));    
   
    mciOpenParms.lpstrElementName=pFileName;    
   
    DWORD dwResult=mciSendCommand(m_nDeviceID,MCI_OPEN,    
        MCI_OPEN_ELEMENT,(DWORD)(LPVOID)&mciOpenParms);    
   
    if(dwResult)    
        DisplayErrorMsg(dwResult);   
    else   
    {   
        m_nElementID=mciOpenParms.wDeviceID;    
        MCI_PLAY_PARMS mciPlayParms;    
        mciPlayParms.dwCallback=(DWORD)hWnd;   
        dwResult=mciSendCommand(m_nElementID,MCI_PLAY,MCI_NOTIFY,(DWORD)(LPVOID)&mciPlayParms);    
   
        if(dwResult)    
        {    
            DisplayErrorMsg(dwResult);    
            Stop();   
        }    
    }   
    return dwResult;    
}    
   
DWORD CQMWave::Stop()    
{   
    DWORD dwResult=0;   
   
    if(m_nElementID)    
    {    
        dwResult=mciSendCommand(m_nElementID,MCI_CLOSE,NULL,NULL);    
   
        if(dwResult)    
            DisplayErrorMsg(dwResult);     
        else    
            m_nElementID=0;    
    }    
    return dwResult;    
}    
   
void CQMWave::DisplayErrorMsg(DWORD dwError)    
{    
    if(dwError)    
    {   
        TCHAR szErrorMsg[MAXERRORLENGTH];    
   
        if(!mciGetErrorString(dwError,szErrorMsg,sizeof(szErrorMsg)))    
            _tcscpy(szErrorMsg,_T("Unknown Error"));
        AfxMessageBox(szErrorMsg);    
    }    
} 